package com.qc31.monitorlib.ui.listmonitor

import android.Manifest
import android.content.Intent
import android.view.View
import android.widget.ImageView
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider
import autodispose2.autoDispose
import com.qc31.baselib.base.LazyFragment
import com.qc31.baselib.databinding.IncludeRecyclerMathMBinding
import com.qc31.baselib.utils.*
import com.qc31.comlib.Keys
import com.qc31.comlib.entity.GpsDetail
import com.qc31.monitorlib.ui.stopcar.StopCarActivity
import com.qc31.monitorlib.R
import com.qc31.monitorlib.ui.alarms.NormalAlarmActivity
import com.qc31.monitorlib.ui.carinfo.CarInfoActivity
import com.qc31.monitorlib.ui.carmonitor.CarMonitorActivity
import com.qc31.monitorlib.ui.line.HistoryLineActivity
import com.qc31.monitorlib.ui.mile.MileActivity
import com.qc31.monitorlib.ui.photo.PhotosActivity
import com.tbruyelle.rxpermissions3.RxPermissions

/** @Author Jasper @Time 2020/11/12 @Desc 列表 */
class ListMonitorFragment :
    LazyFragment<IncludeRecyclerMathMBinding>(IncludeRecyclerMathMBinding::inflate) {
    private val adapter: ListMonitorAdapter by lazy { ListMonitorAdapter() }
    private val itemPopup: ListItemPopup by lazy {
        ListItemPopup(
            requireContext(),
            false,
            clickListener
        )
    }
    private var locationEntity: GpsDetail? = null

    //空布局
    private val emptyView by lazy {
        val view = layoutInflater.inflate(
            R.layout.layout_empty_view,
            binding.recyclerView,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(requireContext(), R.mipmap.ic_empty)
        view
    }

    override fun initView(view: View) {
        binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
        binding.recyclerView.adapter = adapter
        adapter.setEmptyView(emptyView)
    }

    override fun initData() {

    }

    override fun initEvent() {
        adapter.setCallBack(object : ListMonitorAdapter.ListMonitorCallback {
            override fun callBack(item: GpsDetail) {
                locationEntity = item
                itemPopup.showAtLocation(
                    requireActivity().window.decorView,
                    sub(
                        if (adapter.isPlate) {
                            item.carPlate
                        } else {
                            item.carName
                        }, 10
                    )
                )

            }

            override fun mapCallbacks(item: GpsDetail) {
                locationEntity = item
                when (item.state) {
                    "1", "5" -> showToast(R.string.toast_car_state_no_location)
                    else -> toCarMonitor()
                }
            }

        })

    }

    fun setPlatName(boolean: Boolean) {
        adapter.isPlate = boolean
    }

    fun setData(data: MutableList<GpsDetail>) {
        if (data.isEmpty()) {
            adapter.setEmptyView(emptyView)
            adapter.setList(null)
        } else {
            adapter.setList(data)
        }
    }

    fun clear() {
        adapter.data.clear()
        adapter.notifyDataSetChanged()
        adapter.setEmptyView(emptyView)
    }

    fun notifyDataSetChanged() {
        adapter.notifyDataSetChanged()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        adapter.data.clear()
    }

    private val permission by lazy { RxPermissions(this) }
    private val clickListener = View.OnClickListener { v ->
        if (locationEntity == null || isFastClick()) return@OnClickListener
        when (v.id) {
            R.id.tvLMPopLine -> {
                startActivity(HistoryLineActivity::class.java)
            }
            R.id.tvLMPopInfo -> {
                startActivity(
                    Intent(
                        requireContext(),
                        CarInfoActivity::class.java
                    ).putExtra(Keys.INTENT_CAR_ID, locationEntity!!.carId)
                )
//                LiveDataBus.get().with("CarInfo").postValue(locationEntity)
            }
            R.id.tvLMPopMile -> {
                startActivity(MileActivity::class.java)
            }
            R.id.tvLMPopAlarm -> {
                startActivity(NormalAlarmActivity::class.java)
            }
            R.id.tvLMPopPhoto -> {
                startActivity(PhotosActivity::class.java)
            }
            R.id.tvLMPopStopCar -> {
                startActivity(StopCarActivity::class.java)
            }
        }


    }

    private fun toCarMonitor() {
        if (isFastClick()) return
        startToCarMonitor()
    }

    private fun startToCarMonitor() {
        permission.requestEachCombined(
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
        ).autoDispose(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY))
            .subscribe {
                if (it.granted) {
                    startActivityTo<CarMonitorActivity>(requireContext()){
                        putExtra(Keys.INTENT_CAR_ID, locationEntity!!.carId)
                        putExtra(Keys.INTENT_IS_VIDEO, locationEntity!!.videos)
                        putExtra(Keys.INTENT_PLATE, subPlate(locationEntity!!.carPlate, 14))
                    }

                } else {
                    getString(
                        R.string.toast_persission_open,
                        getString(R.string.permission_location)
                    ).toast(
                        requireContext()
                    )
                }
            }

    }

    private fun startActivity(zClass: Class<*>) {
        startActivity(
            Intent(requireContext(), zClass)
                .putExtra(Keys.INTENT_CAR_ID, locationEntity!!.carId)
                .putExtra(Keys.INTENT_PLATE, subPlate(locationEntity!!.carPlate, 14))
        )
    }


}